package com.lingnan.work01.service.impl;

import com.lingnan.work01.entity.Student;
import com.lingnan.work01.dao.StudentDao;
import com.lingnan.work01.service.StudentService;
import com.lingnan.work01.utils.ImportExcelUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * 学生信息表(Student)表服务实现类
 *
 * @author makejava
 * @since 2020-05-27 22:14:12
 */
@Service("studentService")
public class StudentServiceImpl implements StudentService {
    @Resource
    private StudentDao studentDao;

    @Override
    public void impExcel(MultipartFile file) throws Exception {
        // TODO Auto-generated method stub
        System.out.println("............");
        List<Student> impusers = new ArrayList<>();
        if(file.isEmpty()){
            throw new Exception("文件不存在！");
        }
        InputStream in = file.getInputStream();
        List<List<Object>> listob = new ImportExcelUtils().getBankListByExcel(in,file.getOriginalFilename());
        in.close();

        for (int i = 1; i < listob.size(); i++) {
            List<Object> lo = listob.get(i);
            Student student = new Student();
            String id = String.valueOf(lo.get(0));
            student.setId(Integer.parseInt(id));
            student.setStudentId(String.valueOf(lo.get(1)));
            student.setStudentName(String.valueOf(lo.get(2)));
            student.setClassName(String.valueOf(lo.get(3)));
            student.setRemark(String.valueOf(lo.get(4)));
            impusers.add(student);

        }
        System.out.println("impusersSize:"+impusers.size());
        System.out.println(impusers.toString());
        studentDao.impusers(impusers);
    }


    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Student queryById(Integer id) {
        return this.studentDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit 查询条数
     * @return 对象列表
     */
    @Override
    public List<Student> queryAllByLimit(int offset, int limit) {
        return this.studentDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param student 实例对象
     * @return 实例对象
     */
    @Override
    public Student insert(Student student) {
        this.studentDao.insert(student);
        return student;
    }

    /**
     * 修改数据
     *
     * @param student 实例对象
     * @return 实例对象
     */
    @Override
    public Student update(Student student) {
        this.studentDao.update(student);
        return this.queryById(student.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.studentDao.deleteById(id) > 0;
    }
}